﻿using Architecture.API.Application.TraceabilityCommand.ShoppingCommand;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using Traceability.Domain;
using Traceability.Domain.TraceabilityDto;

namespace Architecture.API.Controllers
{
    /// <summary>
    /// API控制器--商品
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ShoppingManagementController : ControllerBase
    {
        /// <summary>
        /// 命令总线
        /// </summary>
        protected readonly IMediator mediator;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mediator">命令总线</param>
        public ShoppingManagementController(IMediator mediator)
        {
            this.mediator = mediator;
        }

        /// <summary>
        /// 添加商品信息
        /// </summary>
        /// <param name="command">请求</param>
        /// <returns>返回受影响的行数</returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddShopping(ShoppingCreateCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 获取商品列表
        /// </summary>
        /// <param name="command">请求</param>
        /// <returns>返回商品列表</returns>
        [HttpPost]
        public async Task<ApiResult<ShoppingPaginationDto<ShowShoppingDto>>> GetShoppingList(ShoppingSeachCommand command)
        {
            ApiResult<ShoppingPaginationDto<ShowShoppingDto>> result = new ApiResult<ShoppingPaginationDto<ShowShoppingDto>>();
            try
            {
                result.Data = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 删除商品信息
        /// </summary>
        /// <param name="command">请求</param>
        /// <returns>返回受影响的行数</returns>
        [HttpPost]
        public async Task<ApiResult<int>> DeletedShopping(ShoppingRemoveCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 反填商品信息
        /// </summary>
        /// <param name="command">请求</param>
        /// <returns>返回商品信息</returns>
        [HttpPost]
        public async Task<ApiResult<ReverseShoppingDto>> BackInfoShopping(ShoppingReverseCommand command)
        {
            ApiResult<ReverseShoppingDto> result = new ApiResult<ReverseShoppingDto>();
            try
            {
                result.Data = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 修改商品信息
        /// </summary>
        /// <param name="command">请求</param>
        /// <returns>返回受影响的行数</returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdateShopping(ShoppingUpdateCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
    }
}
